package com.fgr.controller;

import com.fgr.beans.Animal;
import com.fgr.beans.AnimalType;
import com.fgr.beans.User;
import com.fgr.service.AnimalService;
import com.fgr.service.AnimalTypeService;
import com.fgr.service.UserService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    AnimalService animalService;

    @GetMapping("findAllUser")
    public String findAllUser(Model model){
        List<User> list = userService.selectAllUser();
        model.addAttribute("users",list);
        return "background/user";
    }

    @GetMapping("findAllUserBg2")
    public String findAllUserBg2(Model model){
        List<User> list = userService.selectAllUser();
        model.addAttribute("users",list);
        return "background/addreserve";
    }

    @GetMapping("findAllUserBg3")
    public String findAllUserBg3(Model model){
        List<User> list = userService.selectAllUser();
        model.addAttribute("users",list);
        return "background/addhelp";
    }
    @GetMapping("findAllUserBg4")
    public String findAllUserBg4(Model model){
        List<User> list = userService.selectAllUser();
        model.addAttribute("users",list);
        return "background/addmessage";
    }

    @ResponseBody
    @RequestMapping(path = "registerUser",method = RequestMethod.POST)
    public String insertUser(User user){
        String s = userService.insertUser(user);
        return s;
    }

    @ResponseBody
    @PostMapping("loginUser")
    public String selectUserLogin(User user, HttpServletRequest request){
        String s = userService.selectUser(user, request);
        return s;
    }

    @GetMapping("findUserById")
    public String findUserById(Integer id,Model model){
        User user = userService.findUserById(id);
        model.addAttribute("user",user);
        return "background/modifyuser";
    }

    @ResponseBody
    @PostMapping("deleteUserByIdBg")
    public String deleteUserByIdBg(Integer uid){
        String s = userService.deleteUserById(uid);
        return s;
    }

    @PostMapping("UpdateUser")
    @ResponseBody
    public String UpdateUser(User user){
        String s = userService.UpdateUser(user);
        return s;

    }

    @GetMapping("selectUserAndAnimal")
    public String selectUserAndAnimal(Model model){
        List<Animal> animals = animalService.selectAllAnimal();
        List<User> users = userService.selectAllUser();
        model.addAttribute("animals",animals);
        model.addAttribute("users",users);
        return "background/addadapter";

    }


}
